package com.huawei.hbase.tables;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * 删除表
 */
public class DeleteTable {
    public static void main(String[] args) throws IOException {

        //链接hbase
        Configuration conf = HBaseConfiguration.create();
        //这里的zookeeper地址要改为自己集群的zookeeper地址
        conf.set("hbase.zookeeper.quorum",
                "xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        Connection connection = ConnectionFactory.createConnection(conf);
        Admin hBaseAdmin = connection.getAdmin();

        TableName tableName = TableName.valueOf("users");

        if (hBaseAdmin.tableExists(tableName)) {
            //判断表的状态
            if(hBaseAdmin.isTableAvailable(tableName)) {
                hBaseAdmin.disableTable(tableName);
            }
            hBaseAdmin.deleteTable(tableName);
            System.out.println("删除表"+tableName+"成功");
        }else {
            System.out.println(tableName+"表不存在");
        }
    }
}